package com.cuitrip.component;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;

/**
 * Created by MiJack on 2015/11/30.
 */
public class PayLoadingView extends View {
    private State status;
    private int color;
    private float progess;
    enum State {
        LOADING, SUCCESS, FAILED
    }

    public PayLoadingView(Context context) {
        this(context, null);
    }

    public PayLoadingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PayLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PayLoadingView, defStyleAttr, R.style.PayLoadingView_Default);
        color = a.getColor(R.styleable.PayLoadingView_viewColor, ResourceUtil.getThemeColorRes());
        a.recycle();
        setStatus(State.LOADING);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        switch (status) {
//            case LOADING:
//                drawLoading(canvas);
//                break;
            case SUCCESS:
                drawTick(canvas);
                break;
            case FAILED:
                drawFork(canvas);
                break;
        }
    }

    private void drawTick(Canvas canvas) {
        float length = Math.min(getMeasuredWidth(), getMeasuredHeight());
        drawIcon(length / 3.15f, length / 2, length / 2.06f, length / 1.578f, length / 2.06f, length / 1.578f, length / 1.295f, length / 2.272f, 0.3f, canvas);
    }

    private void drawFork(Canvas canvas) {
        float length = Math.min(getMeasuredWidth(), getMeasuredHeight());
        float x= (float) (length/3.5);
        float y= length -x;
        drawIcon(x, x, y, y, y, x, x, y, 0.5f, canvas);
    }

    private void drawIcon(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float percent, Canvas canvas) {
        if (progess < 0) {
            return;
        }
        Paint paint = new Paint();
        paint.setColor(color);
        paint.setStrokeWidth(getResources().getDisplayMetrics().density * 4);
        float x = x1 + (x2 - x1) * Math.min(progess, percent) / percent;
        float y = y1 + (y2 - y1) * Math.min(progess, percent) / percent;
        canvas.drawLine(x1, y1, x, y, paint);
        if (progess < 0.5) {
            return;
        }
        x = x3 + (x4 - x3) * (progess - percent) / (1 - percent);
        y = y3 + (y4 - y3) * (progess - percent) / (1 - percent);
        canvas.drawLine(x3, y3, x, y, paint);
    }

    private void drawLoading(Canvas canvas) {
//        Paint paint = new Paint();
//        PorterDuff.Mode mPorterDuffMode = PorterDuff.Mode.DST_OUT;
//        PorterDuffXfermode mPorterDuffXfermode = new PorterDuffXfermode(mPorterDuffMode);
//        paint.setXfermode(mPorterDuffXfermode);
//        Log.d("tag", "progess:" + progess);
//        paint.setStrokeWidth(getResources().getDisplayMetrics().density * 2);
//        float start = 360 * progess;
//        float r = Math.min(getMeasuredWidth(), getMeasuredHeight()) / 2;
//        canvas.clipRect((getMeasuredWidth() / 2) - r, (getMeasuredHeight() / 2) - r, (getMeasuredWidth() / 2) + r, getMeasuredHeight() / 2 + r);
//        float r1 = r * 2 / 3;
//        RectF rectF = new RectF((getMeasuredWidth() / 2) - r, (getMeasuredHeight() / 2) - r, (getMeasuredWidth() / 2) + r, getMeasuredHeight() / 2 + r);
//        paint.setColor(color);
//        canvas.drawArc(rectF, 0, start, true, paint);
//        RectF rectF2 = new RectF((getMeasuredWidth() / 2) - r1, (getMeasuredHeight() / 2) - r1, (getMeasuredWidth() / 2) + r1, getMeasuredHeight() / 2 + r1);
//        paint.setColor(Color.WHITE);
//        canvas.drawArc(rectF2, 0, start, true, paint);
    }

    public float getProgess() {
        return progess;
    }

    public State getStatus() {
        return status;
    }

    public void setProgess(float progess) {
        this.progess = progess;
        invalidate();
    }

    public void setStatus(State status) {
        this.status = status;
        invalidate();
    }
}
